﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCentral.Dataset;
using CommonData;
using DevExpress.XtraEditors;

namespace DataCentral.UserControls
{
    public partial class EditSalePersonControl : DataCentral.CommonCenterControl
    {
        public EditSalePersonControl()
        {
            InitializeComponent();
        }

        public override int SaveDataOnControl()
        {
            int iResult = 0;
            try
            {
                this.Validate();
                this.bsSalePerson.EndEdit();
                iResult = daSalePerson.Update(dsDataCentral.Salesperson);
                if (iResult < 0)
                {
                    MessageClass.ShowErrorMessage(ConstantClass._Error_SaveDataError);
                }
                else if (this.Parent is GroupControl)
                {
                    string strCaption = ((GroupControl)Parent).Text;
                    ((GroupControl)Parent).Text = strCaption.Substring(0, strCaption.IndexOf(ConstantClass._STR_START));
                    base.IsDataChanged = false;
                    btnSave.Enabled = false;
                }
            }
            catch (Exception exp)
            {
                //CommonFunctions.AddException("Cập nhật quyền không thành công", exp);
            }
            return iResult;
        }

        private void DataHasChanged()
        {
            base.IsDataChanged = true;
            if (this.Parent is GroupControl && !((GroupControl)Parent).Text.EndsWith(ConstantClass._STR_START))
                ((GroupControl)Parent).Text += ConstantClass._STR_START;
            btnSave.Enabled = true;
        }

        private void SalePersonControl_Load(object sender, EventArgs e)
        {
            daSalePerson.Fill(dsDataCentral.Salesperson);
            ValueListClass clValueList = new ValueListClass();
            iluSaleTeam.DataSource = clValueList.GetSaleTeamList();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveDataOnControl();
        }

        private bool IsSalePersonExisted(DataCentralDataset.SalespersonRow drSalePerson)
        {
            bool isExist = false;
            foreach (DataCentralDataset.SalespersonRow drSelect in dsDataCentral.Salesperson)
            {
                if (drSelect.SalePersonID != drSalePerson.SalePersonID && drSelect.Name.Equals(drSalePerson.Name))
                {
                    isExist = true;
                    break;
                }
            }
            return isExist;
        }

        private void viewSalePerson_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            DataCentralDataset.SalespersonRow salePersonRow;

            if (viewSalePerson.GetFocusedDataRow() is DataCentralDataset.SalespersonRow)
            {
                viewSalePerson.ClearColumnErrors();
                salePersonRow = (DataCentralDataset.SalespersonRow)viewSalePerson.GetFocusedDataRow();

                if (salePersonRow.IsNull("Name") || string.IsNullOrEmpty(salePersonRow.Name))
                {
                    viewSalePerson.SetColumnError(colName, "Tên nhân viên không được để trống");
                    viewSalePerson.FocusedColumn = colName;
                    e.Valid = false;
                }
                //else if (!string.IsNullOrEmpty(salePersonRow.Name) && IsSalePersonExisted(salePersonRow))
                //{
                //    viewSalePerson.SetColumnError(colName, "Tên nhân viên không được trùng nhau");
                //    viewSalePerson.FocusedColumn = colName;
                //    e.Valid = false;
                //}
            }
        }

        private void viewSalePerson_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            DataHasChanged();
        }

        private void viewSalePerson_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                btnDelete.PerformClick();
            }
            else if (e.KeyCode == Keys.Insert)
            {
                btnAdd.PerformClick();
            }
            else if (e.Control && e.KeyCode == Keys.S)
            {
                btnSave.PerformClick();
            }
        }

        private void viewSalePerson_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            MessageClass.ShowErrorMessage(ConstantClass._Error_InvalidData);
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataHasChanged();
        }

        private void viewSalePerson_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            btnDelete.Enabled = (viewSalePerson.GetFocusedDataRow() is DataCentralDataset.SalespersonRow);
        }
    }
}
